home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Graphics / SMan / WarpOS / Source / Packer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-26  |  2.9 KB  |  121 lines

  1. /*----------------------------------------------------------------------*
  2.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *    control bytes:
  8.  *     [0..127]   : followed by n+1 bytes of data.
  9.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *     -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14. #include "iffp/packer.h"
  15. static BYTE *PutDump(BYTE *, int);
  16. static BYTE *PutRun(BYTE *,int,int);
  17.  
  18. #define DUMP    0
  19. #define RUN    1
  20.  
  21. #define MinRun 3
  22. #define MaxRun 128
  23. #define MaxDat 128
  24.  
  25. /* When used on global definitions, static means private.
  26.  * This keeps these names, which are only referenced in this
  27.  * module, from conficting with same-named objects in your program.
  28.  */
  29. static LONG putSize;
  30. static char buf[256];    /* [TBD] should be 128?  on stack?*/
  31.  
  32. #define GetByte()    (*source++)
  33. #define PutByte(c)    { *dest++ = (c);   ++putSize; }
  34.  
  35.  
  36. static BYTE *PutDump(BYTE *dest, int nn)
  37.     {
  38.     int i;
  39.  
  40.     PutByte(nn-1);
  41.     for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  42.     return(dest);
  43.     }
  44.  
  45. static BYTE *PutRun(BYTE *dest, int nn, int cc)
  46.     {
  47.     PutByte(-(nn-1));
  48.     PutByte(cc);
  49.     return(dest);
  50.     }
  51.  
  52. #define OutDump(nn)   dest = PutDump(dest, nn)
  53. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  54.  
  55. /*----------- packrow --------------------------------------------------*/
  56. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  57.  * destination pointers.  RETURNs count of packed bytes.
  58.  */
  59. LONG PackRow(BYTE **pSource, BYTE **pDest, LONG rowSize)
  60.     {
  61.     BYTE *source, *dest;
  62.     char c,lastc = '\0';
  63.     BOOL mode = DUMP;
  64.     short nbuf = 0;        /* number of chars in buffer */
  65.     short rstart = 0;        /* buffer index current run starts */
  66.  
  67.     source = *pSource;
  68.     dest = *pDest;
  69.     putSize = 0;
  70.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  71.     nbuf = 1;   rowSize--;    /* since one byte eaten.*/
  72.  
  73.  
  74.     for (;  rowSize;  --rowSize) {
  75.     buf[nbuf++] = c = GetByte();
  76.     switch (mode) {
  77.         case DUMP:
  78.             /* If the buffer is full, write the length byte,
  79.                then the data */
  80.             if (nbuf>MaxDat) {
  81.                 OutDump(nbuf-1);
  82.                 buf[0] = c;
  83.                 nbuf = 1;   rstart = 0;
  84.                 break;
  85.                 }
  86.  
  87.             if (c == lastc) {
  88.                 if (nbuf-rstart >= MinRun) {
  89.                 if (rstart > 0) OutDump(rstart);
  90.                 mode = RUN;
  91.                 }
  92.                 else if (rstart == 0)
  93.                 mode = RUN;    /* no dump in progress,
  94.                 so can't lose by making these 2 a run.*/
  95.                 }
  96.             else  rstart = nbuf-1;        /* first of run */
  97.             break;
  98.  
  99.         case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  100.                 /* output run */
  101.                OutRun(nbuf-1-rstart,lastc);
  102.                 buf[0] = c;
  103.                 nbuf = 1; rstart = 0;
  104.                 mode = DUMP;
  105.                 }
  106.             break;
  107.         }
  108.  
  109.     lastc = c;
  110.     }
  111.  
  112.     switch (mode) {
  113.     case DUMP: OutDump(nbuf); break;
  114.     case RUN: OutRun(nbuf-rstart,lastc); break;
  115.     }
  116.     *pSource = source;
  117.     *pDest = dest;
  118.     return(putSize);
  119.     }
  120.  
  121.